package com.lcz.authority.admin.mapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.lcz.authority.common.core.mybatis.BaseMapperX;
import com.lcz.authority.common.core.mybatis.LambdaQueryWrapperX;
import com.lcz.authority.common.core.mybatis.MPJLambdaWrapperX;
import com.lcz.authority.common.core.page.PageQuery;
import com.lcz.authority.common.core.page.PageResult;
import com.lcz.authority.common.domain.entity.SysDeptEntity;
import com.lcz.authority.common.domain.entity.SysRoleEntity;
import com.lcz.authority.common.domain.entity.SysUserEntity;
import com.lcz.authority.common.domain.entity.SysUserRoleEntity;
import com.lcz.authority.common.domain.model.LoginUser;
import org.apache.ibatis.annotations.Param;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author lcz
 * @desc 用户映射器
 * @mail lcz_0130@163.com
 * @date 2024/05/06
 */

public interface SysUserMapper extends BaseMapperX<SysUserEntity> {

    default PageResult<SysUserEntity> selectPage(SysUserEntity user) {
        LambdaQueryWrapperX<SysUserEntity> lqw = new LambdaQueryWrapperX<>();
        lqw.select(SysUserEntity.class, i -> !i.getProperty().equals("password"));

        lqw.likeIfPresent(SysUserEntity::getUserName, user.getUserName());
        lqw.likeIfPresent(SysUserEntity::getPhoneNumber, user.getPhoneNumber());
        lqw.eqIfPresent(SysUserEntity::getStatus, user.getStatus());
        lqw.betweenIfPresent(SysUserEntity::getCreateTime, user.getParams());


        return selectPage(lqw);
    }


    /**
     * 通过用户名查询用户
     */
    default SysUserEntity selectUserByUserName(String userName) {
        return selectOne(new MPJLambdaWrapper<SysUserEntity>().selectAll(SysUserEntity.class)
                .eq(SysUserEntity::getDelFlag, "0")
                .eq(SysUserEntity::getUserName, userName));
    }

    /**
     * 通过用户ID查询用户
     *
     * @param userId 用户ID
     * @return 用户对象信息
     */
    default SysUserEntity selectUserById(String userId) {
        return selectOne(new MPJLambdaWrapper<SysUserEntity>().selectAll(SysUserEntity.class)
                .eq(SysUserEntity::getUserId, userId));
    }

    /**
     * 根据条件分页查询已配用户角色列表
     *
     * @return 用户信息集合信息
     */
    default Page<SysUserEntity> selectAllocatedList(SysUserEntity user) {
        Page<SysUserEntity> page = new PageQuery().buildPage();
        MPJLambdaWrapper<SysUserEntity> wrapper = new MPJLambdaWrapper<>();
        wrapper.select(SysUserEntity::getUserId, SysUserEntity::getUserName, SysUserEntity::getNickName, SysUserEntity::getEmail, SysUserEntity::getPhoneNumber, SysUserEntity::getStatus, SysUserEntity::getCreateTime)
                .leftJoin(SysUserRoleEntity.class, SysUserRoleEntity::getUserId, SysUserEntity::getUserId)
                .leftJoin(SysRoleEntity.class, SysRoleEntity::getRoleId, SysUserRoleEntity::getRoleId)
                .eq(SysUserEntity::getDelFlag, "0")
                .eq(SysRoleEntity::getRoleId, user.getRoleId())
                .eqIfExists(SysUserEntity::getPhoneNumber, user.getPhoneNumber())
                .eqIfExists(SysUserEntity::getUserName, user.getUserName())
                .distinct();
        return selectPage(page, wrapper);
    }

    /**
     * 根据条件分页查询未分配用户角色列表
     *
     * @param user 用户信息
     * @return 用户信息集合信息
     */
    default Page<SysUserEntity> selectUnallocatedList(@Param("page") Page<SysUserEntity> page, @Param("user") SysUserEntity user) {
        MPJLambdaWrapperX<SysUserEntity> wrapperX = new MPJLambdaWrapperX<>();
        wrapperX.selectAll(SysUserEntity.class);
        wrapperX.leftJoin(SysUserRoleEntity.class, SysUserRoleEntity::getUserId, SysUserEntity::getUserId);
        wrapperX.leftJoin(SysRoleEntity.class, SysRoleEntity::getRoleId, SysUserRoleEntity::getRoleId);
        wrapperX.eq(SysUserEntity::getDelFlag, "0");
        wrapperX.ne(SysRoleEntity::getRoleId, user.getRoleId());
        wrapperX.or().isNull(SysRoleEntity::getRoleId);
        wrapperX.likeIfPresent(SysUserEntity::getUserName, user.getUserName());
        wrapperX.likeIfPresent(SysUserEntity::getPhoneNumber, user.getPhoneNumber());
        List<SysUserEntity> sysUserEntities = selectList(wrapperX);
        MPJLambdaWrapperX<SysUserEntity> wrapper = new MPJLambdaWrapperX<>();
        wrapper.select(SysUserEntity::getUserId).innerJoin(SysUserRoleEntity.class, SysUserRoleEntity::getUserId, SysUserEntity::getUserId).eq(SysUserRoleEntity::getRoleId, user.getRoleId());
        List<String> collect1 = selectList(wrapper).stream().map(SysUserEntity::getUserId).collect(Collectors.toList());
        sysUserEntities.removeIf(sysUserEntity -> collect1.contains(sysUserEntity.getUserId()));
        return page.setRecords(sysUserEntities);
    }


    /**
     * 修改用户头像
     *
     * @param loginUser 用户信息
     * @param avatar    头像地址
     * @return 结果
     */
    default int updateUserAvatar(@Param("loginUser") LoginUser loginUser, @Param("avatar") String avatar) {
        SysUserEntity sysUserEntity = new SysUserEntity();
        sysUserEntity.setAvatar(avatar);
        sysUserEntity.setUserId(loginUser.getUser().getUserId());
        return updateById(sysUserEntity);
    }

    /**
     * 校验email是否唯一
     *
     * @param email 用户邮箱
     * @return 结果
     */
    default SysUserEntity checkEmailUnique(String email) {

        return selectOne(new LambdaQueryWrapperX<SysUserEntity>().eq(SysUserEntity::getEmail, email));
    }

    /**
     * 校验手机号码是否唯一
     *
     * @param phoneNumber 手机号码
     * @return 结果
     */
    default SysUserEntity checkPhoneUnique(String phoneNumber) {
        return selectOne(new LambdaQueryWrapperX<SysUserEntity>().eq(SysUserEntity::getPhoneNumber, phoneNumber));
    }

    /**
     * 重置用户密码
     *
     * @param user     用户信息
     * @param password 密码
     * @return 结果
     */
    default int resetUserPwd(@Param("user") SysUserEntity user, @Param("password") String password) {
        user.setPassword(password);
        return updateById(user);
    }


    /**
     * 校验用户名称是否唯一
     *
     * @param userName 用户名称
     * @return 结果
     */
    default SysUserEntity checkUserNameUnique(String userName) {
        return selectOne(new LambdaQueryWrapperX<SysUserEntity>().eq(SysUserEntity::getUserName, userName));
    }

    /**
     * 查询部门是否存在用户
     *
     * @param deptId 部门ID
     * @return 结果
     */
    default int checkDeptExistUser(Long deptId) {
        //		select count(1) from sys_user where dept_id = #{deptId} and del_flag = '0'
        return Math.toIntExact(selectCount(new MPJLambdaWrapperX<SysUserEntity>().eq(SysDeptEntity::getDeptId, deptId)
                .eq(SysDeptEntity::getDelFlag, "0")
        ));
    }



}
